<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>后台管理-注册</title>
		<!-- 样 式 文 件 -->
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
		<link rel="stylesheet" href="/static/ui/css/ui.css" />
		<link rel="stylesheet" href="/static/ui/admin/css/other/login.css" />
        <style>
            .layui-form{
                margin-top: 10px !important;
            }
        </style>
	</head>
    <!-- 代 码 结 构 -->
	<body background="/static/ui/admin/images/background.svg" style="background-size: cover;">
		<form class="layui-form" action="javascript:void(0);">
			<div class="layui-form-item">
				<img class="logo" src="/static/logo.png" />
				<div class="title">GodoOA</div>
			</div>
			<div class="layui-form-item">
				<input placeholder="账 户" value="" name="username" lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input placeholder="密 码" name="password" value="" lay-verify="required" hover class="layui-input" type="password" />
			</div>
            <div class="layui-form-item">
				<input placeholder="重复密码" name="repassword" value="" lay-verify="required" hover class="layui-input" type="password" />
			</div>
            <div class="layui-form-item">
				<input placeholder="姓名" value="" name="name" lay-verify="required" hover class="layui-input"  />
			</div>
			<!--div class="layui-form-item">
				<input placeholder="联系人" value="" name="contact" lay-verify="required" hover class="layui-input"  />
			</div-->
			<div class="layui-form-item">
				<input placeholder="电话" value="" name="tel" lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input placeholder="验证码" name="captcha" id="captcha"  hover  lay-verify="required" class="code layui-input layui-input-inline"  />
				<img src="" id="codeImage" class="codeImage" />
				<input type="hidden" value="" name="codekey" id="codekey">
			</div>
			<div class="layui-form-item">
				<input type="checkbox" name="remberpwd" title="记住密码" lay-skin="primary" checked>
			</div>
			<div class="layui-form-item">
				<button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					注 册
				</button>
			</div>
			

		</form>
        <div class="layui-form-item">
            <div class="desc">
                <a href="login.html">登录</a>
            </div>
        </div>
		<!-- 资 源 引 入 -->
		<script src="/res/group/config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['form', 'button', 'popup'], function() {
				let form = layui.form,
					button = layui.button,
					popup = layui.popup,
					$ = layui.$,
					layer = layui.layer;
					changeImg = () => {
						//$('#codeImage').attr('src',apiUrl + 'login/captcha?t='+ Math.random());
						let url = apiUrl + 'login/captcha';

						$.get(url, res => {
							// 通过window.btoa() 方法用于创建一个 base-64 编码的字符串
							const img = `data:image/svg+xml;base64,${window.btoa(res.data.svg)}`;
							$('#codeImage').attr('src',img);
							$('#codekey').val(res.data.key)
						})
					}
				changeImg();
				$('#codeImage').click(() => {
					changeImg();
				});
				$('#captcha').on('keydown', (e) => {
					if (e.keyCode == 13) {
						$(".login").trigger("click");
						return false
					}
				});
                // 登 录 提 交
				form.on('submit(login)', function(data) {
					
					/// 验证
					data = data.field;
					//console.log(data);
					if (data.username == '') {
						layer.msg('用户名不能为空');
						return false;
					}
					if (data.password == '') {
						layer.msg('密码不能为空');
						return false;
					}
                    if(data.password.length < 6) {
                        layer.msg('密码长度不能小于6位');
						return false;
                    }
                    if (data.repassword != data.password) {
						layer.msg('两次密码输入不一致');
						return false;
					}
					if (data.captcha == '') {
						layer.msg('验证码不能为空');
						return false;
					}
					/// 登录
					const index = layer.load(0, {
						time: 5 * 1000
					}); //显示加载中动画，最多5秒自动关闭
					let postData = {
						password: data.password,
						username: data.username,
                        name : data.name,
                        tel : data.tel,
                        //contact : data.contact,
						captcha: data.captcha,
						codekey : data.codekey
					};
					_post(layui, 'login/regDo', postData, res => {
						layer.close(index);
						//console.log(res)
						//setToken(res);
						layer.msg('注册成功！');
						location.href = './login.html';
					}, err => {
						layer.close(index);
						//console.log(err)
						if(err.code == 201) {
							changeImg();
						}
					})
				
					return false;
				});
			})
		</script>
	</body>
</html>
